package com.yyx.app1.Question_function.Control.QuestionListView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.yyx.app1.Data.BaseDataManager;
import com.yyx.app1.Data.Question.Question_Activity_Data;
import com.yyx.app1.Data.testDate;
import com.yyx.app1.MyApp;
import com.yyx.app1.Question_function.UI.Chat_Activity_;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by yuexing on 2015/9/15.
 */
public class ListView_Question_Adapter extends BaseAdapter {
    private List<HashMap<String,Object>> list;
    private Context context;
    private final int TYPE_COUNT=2;
    private final int FIRST_TYPE=0;
    private final int OTHERS_TYPE=1;
    private int position= -1;

    public ListView_Question_Adapter(Context context) {
        this.context = context;
    }

    public void findAll()
    {
        String portraitURL = new testDate().strings[9];
        list=new ArrayList<>();
        HashMap<String,Object> map2 =new HashMap<>();
        String[] imagelist2=new testDate().strings;
        Question_Activity_Data data2=new Question_Activity_Data("04", Question_Activity_Data.QuestionStatusType.NotBeginning,"俞飞鸿","爱斯基摩犬",portraitURL,"宝贝前4天我带它去医务室玩，它在地板上流了比黄豆小一点的血，我以为没事，" +
                "结果最近几天妈妈拖地时经常看到血 ，妈妈以为秋秋大姨妈来了",imagelist2, "2014/12/23 14:38:15");
        map2.put("value",data2);
        map2.put("position",0);
        list.add(map2);
        for (int i = 1 ; i<new testDate().strings.length; ++i)
        {
            HashMap<String,Object> map =new HashMap<>();
            String[] imagelist=new String[]{new testDate().strings[i]};
            Question_Activity_Data data=new Question_Activity_Data("04", Question_Activity_Data.QuestionStatusType.NotBeginning,"username","加菲猫",portraitURL,"宝贝前4天我带它去医务室玩，它在地板上流了比黄豆小一点的血，我以为没事，" +
                    "结果最近几天妈妈拖地时经常看到血 ，妈妈以为秋秋大姨妈来了",imagelist, "9-23 15:30:15");
            map.put("value",data);
            map.put("position",i);
            list.add(map);
        }
        notifyDataSetChanged();
    }
    public void delete()
    {
        list.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount()
    {
        if (list==null)
        {
            return 1;
        }
        else
        {
            return list.size()+1;
        }
    }

    @Override
    public Object getItem(int position)
    {
        if (list==null)
        {
            return null;
        }
        else
        {
            if (position==0)
            {
                return null;
            }
            else
            {
                return list.get(--position);
            }
        }
    }

    @Override
    public long getItemId(int position2) {
        return position2;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0)
        {
            return FIRST_TYPE;
        }
        else
        {
            return OTHERS_TYPE;
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try
        {
            ListViewHead ithead;
            Item_ListView_Question itMessage ;
            int currentType= getItemViewType(position);
            if (currentType == FIRST_TYPE)
            {
                if (convertView == null)
                {
                    ithead = ListViewHead_.build(context);
                }
                else
                {
                    ithead = (ListViewHead)convertView;
                }
                String str = BaseDataManager.getMyBOSSInfo(context).getPortraitURL();
                if (str != null)
                    ithead.bind(str);
                return ithead;
            }
            else
            {
                if (convertView==null)
                {
                    itMessage=Item_ListView_Question_.build(context);
                }
                else
                {
                    itMessage=(Item_ListView_Question)convertView;
                }
                itMessage.bindView((Question_Activity_Data)list.get(position-1).get("value"));
                itMessage.getButton_goto_chat().setTag(list.get(position-1));
                itMessage.setButtonAddOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, Chat_Activity_.class);
                        Bundle bundle = new Bundle();
                        HashMap<String,Object> hashMap = (HashMap<String, Object>) v.getTag();
                        setPosition((int) hashMap.get("position")+1 );
                        bundle.putSerializable("Data", (Serializable) hashMap.get("value"));
                        intent.putExtras(bundle);
                        context.startActivity(intent);
                    }
                });
                return itMessage;
            }
        }
        catch (Exception e)
        {
            Log.e("异常1510151640",e.toString());
            return null;
        }
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }
}
